package com.hwd.gmall.seckill.config;

import com.hwd.gmall.common.constant.Const;
import com.hwd.gmall.common.constant.SeckillConst;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 秒杀超时订单的消息队列的配置
 *
 * @author 黄伟东/Victor
 * @date 2022/5/9 16:44
 */
@Configuration
public class SeckillOrderRabbitConfig {

    /**
     * 正常交换机
     * @return
     */
    @Bean("seckillOrderNormalExchange")
    public Exchange seckillOrderNormalExchange(){
        return ExchangeBuilder.directExchange(SeckillConst.SECKILL_ORDER_NORMAL_EXCHANGE).build();
    }

    /**
     * 死信队列
     * @return
     */
    @Bean("seckillOrderDeadQueue")
    public Queue seckillOrderDeadQueue(){
        return QueueBuilder.durable(SeckillConst.SECKILL_ORDER_DEAD_QUEUE)
                .withArgument(Const.X_DEAD_LETTER_EXCHANGE, SeckillConst.SECKILL_ORDER_DEAD_EXCHANGE)
                .withArgument(Const.X_DEAD_LETTER_ROUTING_KEY, SeckillConst.SECKILL_ORDER_NORMAL_ROUTING_KEY)
                .build();
    }

    /**
     * 死信队列和正常交换机绑定
     * @param seckillOrderNormalExchange
     * @param seckillOrderDeadQueue
     * @return
     */
    @Bean
    public Binding seckillOrderDeadBinding(@Qualifier("seckillOrderNormalExchange") Exchange seckillOrderNormalExchange,
                                           @Qualifier("seckillOrderDeadQueue") Queue seckillOrderDeadQueue){
        return BindingBuilder.bind(seckillOrderDeadQueue)
                .to(seckillOrderNormalExchange)
                .with("seckill.order.dead")
                .noargs();
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean("seckillOrderDeadExchange")
    public Exchange seckillOrderDeadExchange(){
        return ExchangeBuilder.directExchange(SeckillConst.SECKILL_ORDER_DEAD_EXCHANGE).build();
    }

    /**
     * 正常队列
     * @return
     */
    @Bean("seckillOrderNormalQueue")
    public Queue seckillOrderNormalQueue(){
        return QueueBuilder.durable(SeckillConst.SECKILL_ORDER_NORMAL_QUEUE).build();
    }

    /**
     * 正常队列和死信交换机绑定
     * @param seckillOrderDeadExchange
     * @param seckillOrderNormalQueue
     * @return
     */
    @Bean
    public Binding seckillOrderNormalBinding(@Qualifier("seckillOrderDeadExchange") Exchange seckillOrderDeadExchange,
                                           @Qualifier("seckillOrderNormalQueue") Queue seckillOrderNormalQueue){
        return BindingBuilder.bind(seckillOrderNormalQueue)
                .to(seckillOrderDeadExchange)
                .with(SeckillConst.SECKILL_ORDER_NORMAL_ROUTING_KEY)
                .noargs();
    }



}
